<template>
  <div class="tab-title tab-title-smooth" ref="navList">
    <b class="nav-bar" :style="{transform: 'translateX('+translateVal+'px)', width: linewidth+'px'}"></b>
    <div class="title-nav-scroll" :class="{'tab-active':curIndex===index}" v-for="(item,index) in tabList" :key="index" ref="navItem">
      <div class="tab-link" @click="tabClick(index)"> {{item.name}}</div>
    </div>
  </div>
</template>
<script>
export default {
  name: "TabNav",
  describe: 'tab选项卡，自定义',
  props: {
    linewidth: { type: Number, default: 60 },//底部滑动条宽度
    tabList: { type: Array },//tab标题
    curIndex:{type:Number | String}
  },
  data() {
    return {
      translateVal: 20,
      leftVal: 0,
    }
  },

  methods: {
    tabClick(val) {
      this.$nextTick(() => {
        let width = this.$refs.navItem[0].offsetWidth;//获取div块的宽度
        this.translateVal = width * val + 20
        this.$refs.navList.scrollLeft = (val - 1) * width;
      });
      this.$emit("curTab",val)
      
    },
  }
};
</script>
<style lang="scss" scope>
::-webkit-scrollbar {
  display: none !important;
  width: 0px;
  height: 0px;
}
.tab-title-smooth {
  scroll-behavior: smooth;
  background: #fff;
  border-radius: 10px;
  padding: 10px;
}
.tab-title {
  width: 100%;
  display: flex;
  height: 50px;
  line-height: 48px;
  box-sizing: border-box;
  position: relative;
  overflow-x: auto;
  overflow-y: hidden;
  .nav-bar {
    position: absolute;
    height: 2px;
    bottom: 0px;
    left: 0px;
    z-index: 2;
    background: #64aaf1;
    transition: all 0.3s ease-in-out;
    border-radius: 2px;
  }
  .title-nav-scroll {
    min-width: 80px;
    position: relative;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    display: flex;

    box-sizing: border-box;
  }
  .tab-link {
    color: #333;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    text-decoration: none;
    line-height: 1;
    width: 100%;
    height: 100%;
  }
  .tab-active {
    border: 0;
    .tab-link {
      color: #64aaf1;
    }
  }
}
</style>